package com.yxinsur.imc.business.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxinsur.imc.business.domain.ImcBroker;
import com.yxinsur.imc.business.domain.ImcBrokerRole;
import com.yxinsur.imc.business.domain.ImcTeam;
import com.yxinsur.imc.business.mapper.ImcBrokerMapper;
import com.yxinsur.imc.business.mapper.ImcBrokerRoleMapper;
import com.yxinsur.imc.business.mapper.ImcTeamMapper;
import com.yxinsur.imc.business.vo.*;
import com.yxinsur.imc.common.core.domain.entity.SysOrg;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.StringUtils;
import com.yxinsur.imc.system.mapper.SysOrgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yxinsur.imc.business.mapper.ImcBrokerRelationMapper;
import com.yxinsur.imc.business.domain.ImcBrokerRelation;
import com.yxinsur.imc.business.service.IImcBrokerRelationService;
import org.springframework.util.ObjectUtils;

/**
 * 代理人关系Service业务层处理
 *
 * @author imc
 * @date 2021-03-18
 */
@Service
public class ImcBrokerRelationServiceImpl extends ServiceImpl<ImcBrokerRelationMapper, ImcBrokerRelation> implements IImcBrokerRelationService
{
    @Autowired
    private ImcBrokerRelationMapper imcBrokerRelationMapper;

    @Autowired
    private ImcBrokerMapper imcBrokerMapper;

    @Autowired
    private ImcTeamMapper imcTeamMapper;
    @Autowired
    private SysOrgMapper sysOrgMapper;
    @Autowired
    private ImcBrokerRoleMapper imcBrokerRoleMapper;

    /**
     * 查询代理人关系
     *
     * @param id 代理人关系ID
     * @return 代理人关系
     */
    @Override
    public ImcBrokerRelation selectImcBrokerRelationById(String id)
    {
        return imcBrokerRelationMapper.selectImcBrokerRelationById(id);
    }

    /**
     * 查询代理人关系列表
     *
     * @param imcBrokerRelation 代理人关系
     * @return 代理人关系
     */
    @Override
    public List<ImcBrokerRelation> selectImcBrokerRelationList(ImcBrokerRelation imcBrokerRelation)
    {
        return imcBrokerRelationMapper.selectImcBrokerRelationList(imcBrokerRelation);
    }

    /**
     * 新增代理人关系
     *
     * @param imcBrokerRelation 代理人关系
     * @return 结果
     */
    @Override
    public int insertImcBrokerRelation(ImcBrokerRelation imcBrokerRelation)
    {
        imcBrokerRelation.setCreateTime(DateUtils.getNowDate());
        return imcBrokerRelationMapper.insertImcBrokerRelation(imcBrokerRelation);
    }

    /**
     * 修改代理人关系
     *
     * @param imcBrokerRelation 代理人关系
     * @return 结果
     */
    @Override
    public int updateImcBrokerRelation(ImcBrokerRelation imcBrokerRelation)
    {
        imcBrokerRelation.setUpdateTime(DateUtils.getNowDate());
        return imcBrokerRelationMapper.updateImcBrokerRelation(imcBrokerRelation);
    }

    /**
     * 批量删除代理人关系
     *
     * @param ids 需要删除的代理人关系ID
     * @return 结果
     */
    @Override
    public int deleteImcBrokerRelationByIds(String[] ids)
    {
        return imcBrokerRelationMapper.deleteImcBrokerRelationByIds(ids);
    }

    /**
     * 删除代理人关系信息
     *
     * @param id 代理人关系ID
     * @return 结果
     */
    @Override
    public int deleteImcBrokerRelationById(String id)
    {
        return imcBrokerRelationMapper.deleteImcBrokerRelationById(id);
    }

    /**
     * 获取所辖
     * @param brokerId
     * @return
     */
    @Override
    public List<BrokerRelationVo> treeDataWithoutUp(Long brokerId) {
        List<BrokerRelationVo> brokerIdList = imcBrokerRelationMapper.getBrokerDownBrokersByBrokerId(brokerId);
        //获取代理人信息
        for (BrokerRelationVo brokerRelationVo : brokerIdList) {
            //营销区信息，工号，名称，是否在职
            Long relationId = brokerRelationVo.getBrokerId();
            ImcBroker imcBroker = imcBrokerMapper.selectImcBrokerById(String.valueOf(relationId));
            ImcTeam imcTeam = imcTeamMapper.selectImcTeamById(imcBroker.getTeamId());
            brokerRelationVo.setBrokerCode(imcBroker.getBrokerCode());
            brokerRelationVo.setFullName(imcBroker.getFullName());
            brokerRelationVo.setStatus(imcBroker.getStatus());
            brokerRelationVo.setEmployDate(imcBroker.getEmployDate());
            brokerRelationVo.setTeamName(imcTeam.getTeamName());
            ImcBroker brokerUp = imcBrokerMapper.selectImcBrokerById(String.valueOf(brokerRelationVo.getBrokerUpId()));
            if(!ObjectUtils.isEmpty(brokerUp)){
                brokerRelationVo.setBrokerUpName(brokerUp.getFullName());
                brokerRelationVo.setBrokerUpCode(brokerUp.getBrokerCode());
            }
            brokerRelationVo.setOrgId(String.valueOf(imcTeam.getOrgId()));
            SysOrg sysOrg = sysOrgMapper.selectOrgById(imcTeam.getOrgId());
            brokerRelationVo.setOrgName(sysOrg.getOrgName());
            List<BrokerRoleVo> brokerRoles = imcBrokerRoleMapper.selectListByBrokerId(relationId);
            brokerRelationVo.setRoles(brokerRoles);
        }
        return convertTree(brokerIdList,String.valueOf(brokerId));
    }

    public List<BrokerRelationVo> convertTree(List<BrokerRelationVo> list, String parentId) {
        List<BrokerRelationVo> trees = new ArrayList<>();
        for (BrokerRelationVo item : list) {
            //获取到根节点
            if (parentId.equals(String.valueOf(item.getBrokerId()))) {
                trees.add(item);
            }
            //遍历获取所有节点下的子节点数据，去除子节点列表中的重复数据
            for (BrokerRelationVo it : list) {
                if (it.getBrokerUpId().equals(item.getBrokerId())) {
                    if (item.getChildren() == null) {
                        item.setChildren(new ArrayList<BrokerRelationVo>());
                    }
                    boolean isPut = true;
                    for (BrokerRelationVo childItem : item.getChildren()) {
                        if (it.getBrokerId().equals(childItem.getBrokerId())) {
                            isPut = false;
                        }
                    }
                    if (isPut) {
                        item.getChildren().add(it);
                    }

                }
            }
        }
        return trees;
    }


    /**
     * 获取人员关系图
     * @param brokerId
     * @return
     */
    @Override
    public BrokerRelationJsonVo relationDiagram(Long brokerId) {
        List<BrokerRelationVo> brokerIdList = imcBrokerRelationMapper.getBrokerDownBrokersByBrokerId(brokerId);
        List<BrokerRelationVo> brokerUpList = imcBrokerRelationMapper.getBrokerUpBrokersByBrokerId(brokerId);
        BrokerRelationJsonVo brokerRelationJsonVo = new BrokerRelationJsonVo();
        ArrayList<BrokerNodeVo> brokerNodeVos = new ArrayList<>();
        ArrayList<BrokerLinkVo> brokerLinkVos = new ArrayList<>();
        Collections.sort(brokerUpList);
        //保存rootid信息以及节点的上两级信息
        BrokerNodeVo brokerNodeVoTemp = new BrokerNodeVo();
        brokerNodeVoTemp.setId(0L);
        brokerNodeVoTemp.setText("");
        brokerNodeVos.add(brokerNodeVoTemp);
        for (BrokerRelationVo brokerUpVo : brokerUpList) {
            BrokerNodeVo brokerNodeVo = new BrokerNodeVo();
            BrokerLinkVo brokerLinkVo = new BrokerLinkVo();
            Long brokerRootId = brokerUpVo.getBrokerId();
            if(brokerRelationJsonVo.getRootId()==null){
                brokerRelationJsonVo.setRootId(brokerRootId);
            }
            //保存node
            ImcBroker imcBroker = imcBrokerMapper.selectImcBrokerById(String.valueOf(brokerRootId));
            brokerNodeVo.setId(brokerRootId);
            brokerNodeVo.setText(imcBroker.getFullName());
            brokerNodeVos.add(brokerNodeVo);
            //保存link信息
            brokerLinkVo.setFrom(String.valueOf(brokerUpVo.getBrokerUpId()));
            brokerLinkVo.setTo(String.valueOf(brokerUpVo.getBrokerId()));
            brokerLinkVos.add(brokerLinkVo);
        }
        //保存所辖人员信息
        for (BrokerRelationVo brokerRelationVo : brokerIdList) {
            BrokerNodeVo brokerNodeVo = new BrokerNodeVo();
            BrokerLinkVo brokerLinkVo = new BrokerLinkVo();
            //保存node信息
            Long relationId = brokerRelationVo.getBrokerId();
            if(relationId.equals(brokerId)){
                continue;
            }
            ImcBroker imcBroker = imcBrokerMapper.selectImcBrokerById(String.valueOf(relationId));
            brokerNodeVo.setId(relationId);
            brokerNodeVo.setText(imcBroker.getFullName());
            brokerNodeVos.add(brokerNodeVo);
            //保存link信息
            brokerLinkVo.setFrom(String.valueOf(brokerRelationVo.getBrokerUpId()));
            brokerLinkVo.setTo(String.valueOf(brokerRelationVo.getBrokerId()));
            brokerLinkVos.add(brokerLinkVo);

        }
        brokerRelationJsonVo.setNodes(brokerNodeVos);
        brokerRelationJsonVo.setLinks(brokerLinkVos);
        return brokerRelationJsonVo;
    }

    @Override
    public ImcBrokerRelation selectImcBrokerRelationByBrokerId(ImcBrokerRelation imcBrokerRelation) {
        return imcBrokerRelationMapper.selectImcBrokerRelationByBrokerId(imcBrokerRelation.getBrokerId());
    }



}
